package com.young.rabbitmq.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConnectionUtil {
    public static Connection buildConnection(){
        //1、创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //2、设置主机和端口，默认是localhost 和 5672
        connectionFactory.setHost("192.168.0.101");
        connectionFactory.setPort(5672);
        //3、设置虚拟主机
        connectionFactory.setVirtualHost("rabbit");
        //4、设置用户名和密码
        connectionFactory.setUsername("rabbit");
        connectionFactory.setPassword("rabbit");
        //5、建立连接
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            //6、创建通道,一个连接中可以包含多个通道
            Channel channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return connection;
    }
}
